<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class detail_symptom extends MX_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('medical_disease_model');
        $this->load->model('medical_parts_model');
        $this->load->model('medical_department_model');
        $this->load->model('medical_symptoms_model');
        $this->load->model('medical_object_model');
        $this->load->model('cms_tag_model');
        $this->load->model('cms_category_model');
        $this->load->model('admin_model');
    }

    protected function _eventLoadLayout() {
        $this->page['title'] = 'Giải pháp y tế cộng đồng';
        $this->module['title'] = 'Giải pháp y tế cộng đồng';
    }

    function index($id = '') {
        $data = array();
        try{
            $symptom_detail = $this->medical_symptoms_model->load_by_id($id);

            if(!isset($symptom_detail->id)){
                redirect(site_url('index', true));
            }

            //danh mục bệnh là khoa luôn
            $result_dep = $this->medical_disease_model->db->query("SELECT * FROM medical_department WHERE is_hidden = 1");
            $list_dep   = array();
            foreach ($result_dep->result() as $row) {
                $row->href          = BASE_URL . renderToAlias($row->name) . '-dp'.$row->id . '.html';
                $list_dep[$row->id] = $row;
            }
            $data['menu_medical'] = $list_dep;

            //danh mục tin tức
            $result_cat = $this->medical_disease_model->db->query("SELECT * FROM cms_category WHERE is_active = 1");
            $list_cat   = array();
            foreach ($result_cat->result() as $row) {
                $row->href          = BASE_URL . renderToAlias($row->name) . '-ca'.$row->id . '.html';
                $list_cat[$row->id] = $row;
            }
            $data['menu_news'] = $list_cat;

            //tin tức tieu diem (lấy những thằng hot_article mới nhất)
            $query_article = 'SELECT id, title, image, description, updated_at  FROM cms_article WHERE hot_article = 1 AND status = 1 ORDER BY id DESC LIMIT 4';
            $res_article   = $this->medical_disease_model->db->query($query_article);
            $hot_article   = array();
            if($res_article){
                foreach ($res_article->result() as $row) {
                    $row->image = ($row->image) ? render_src($row->image) : BASE_URL. "themes/standard/images/top_test.jpg";
                    $row->date  = date('d-m-Y', $row->updated_at);
                    $hot_article[$row->id] = $row;
                }
            }

            //tin tức mới nhất
            $query_news   = 'SELECT id, title, image, description, updated_at  FROM cms_article WHERE status = 1 ORDER BY id DESC LIMIT 4';
            $res_news     = $this->medical_disease_model->db->query($query_news);
            $news_article = array();
            if($res_news){
                foreach ($res_news->result() as $row) {
                    $row->image = ($row->image) ? render_src($row->image) : BASE_URL. "themes/standard/images/top_test.jpg";
                    $row->date  = date('d-m-Y', $row->updated_at);
                    $news_article[$row->id] = $row;
                }
            }

            //bệnh liên quan đến triệu chứng
            $disease_relate = array();
            $disease_res    = $this->medical_disease_model->db->query('SELECT * FROM medical_disease WHERE CONCAT(",",symptoms_ids,",") LIKE "%' .  $symptom_detail->id . '%" AND is_hidden = 1 ORDER BY id DESC LIMIT 4');
            if($disease_res){
                foreach ($disease_res->result() as $row) {
                    $row->href         = BASE_URL . 'benh/' . $row->alias . '-ds'.$row->id . '.html';
                    $row->image        = ($row->image) ? render_src($row->image) : BASE_URL. "themes/standard/images/top_test.jpg";
                    $row->date         = (isset($row->update_date)) ? date("d-m-Y", $row->update_date) : date('d-m-Y', time());
                    $disease_relate[$row->id]     = $row;
                }
            }

            //lấy bệnh mới nhất làm mục bạn nên xem
            $res_dis_new = $this->medical_disease_model->db->query('SELECT id, name, alias, image, short_description FROM medical_disease WHERE is_hidden = 1 ORDER BY id DESC LIMIT 4');
            $disease_new = array();
            foreach ($res_dis_new->result() as $row) {
                $row->image        = ($row->image) ? render_src($row->image) : BASE_URL. "themes/standard/images/top_test.jpg";
                $row->href         = BASE_URL . 'benh/' . $row->alias . '-ds'.$row->id . '.html';
                $row->date         = (isset($row->update_date)) ? date("d-m-Y", $row->update_date) : date('d-m-Y', time());
                $disease_new[$row->id] = $row;
            }
            $data['disease_new'] = $disease_new;


            //lấy triệu trứng mới nhất làm mục triệu chứng cần biết
            $query_sym_new = 'SELECT id, name, created_date FROM medical_symptoms WHERE is_hidden = 1 ORDER BY id DESC LIMIT 4';
            $res_sym_new   = $this->medical_disease_model->db->query($query_sym_new);
            $sym_new       = array();
            if($res_sym_new){
                foreach ($res_sym_new->result() as $row) {
                    $row->href  = BASE_URL . 'trieu-chung/' . renderToAlias($row->name) . '-st'.$row->id . '.html';
                    $row->date  = (isset($row->created_date)) ? date('d-m-Y', $row->created_date) : date('d-m-Y', time);
                    $sym_new[$row->id] = $row;
                }
            }
            $data['sym_new'] = $sym_new;

            $symptom_detail->create_date = (isset($symptom_detail->created_date)) ? date('d-m-Y', $symptom_detail->created_date) : date('d-m-Y', time());
            if(isset($symptom_detail->user_id) && $symptom_detail->user_id){
                $user = $this->admin_model->load_by_id($symptom_detail->user_id);
                $symptom_detail->username = $user->name;
            }else{
                $symptom_detail->username = 'Admin';
            }

            $data['hot_article']    = $hot_article;
            $data['news_article']   = $news_article;
            $data['symptom_detail'] = $symptom_detail;
            $data['disease_relate'] = $disease_relate;
            $this->setPageContent('home/detail_symp_tpl', $data);
            $this->renderLayout('layouts/layout_detail');
        } catch (Exception $ex) {
            echo $ex->getMessage();
        }
    }

}